#include "DY_SV17F.h"
#include "bflb_gpio.h"
#include "bflb_uart.h"

#define DYSV17F_PutChar(byte)       bflb_uart_putchar(uartx,byte)
#define DYSV17F_Get(data,length)    bflb_uart_get(uartx,data,length)
static struct bflb_device_s *uartx;


void DYSV17F_Uart_Init(void)
{
    struct bflb_device_s *gpio;

    gpio = bflb_device_get_by_name("gpio");

    bflb_gpio_uart_init(gpio, GPIO_PIN_21, GPIO_UART_FUNC_UART0_TX);
    bflb_gpio_uart_init(gpio, GPIO_PIN_22, GPIO_UART_FUNC_UART0_RX);

    uartx = bflb_device_get_by_name("uart0");

    struct bflb_uart_config_s cfg;

    cfg.baudrate = 9600;
    cfg.data_bits = UART_DATA_BITS_8;
    cfg.stop_bits = UART_STOP_BITS_1;
    cfg.parity = UART_PARITY_NONE;
    cfg.flow_ctrl = 0;
    cfg.tx_fifo_threshold = 7;
    cfg.rx_fifo_threshold = 7;
    bflb_uart_init(uartx, &cfg);
}

void DYSV17F_ControlCommand(uint8_t CommandType)
{
    DYSV17F_PutChar(0xAA);
    DYSV17F_PutChar(CommandType);
    DYSV17F_PutChar(0x00);
    switch (CommandType)
    {
        case ControlCommand_Start:
            DYSV17F_PutChar(0xAC);
            break;
        case ControlCommand_Pause:
            DYSV17F_PutChar(0xAD);
            break;
        case ControlCommand_Stop:
            DYSV17F_PutChar(0xAE);
            break;
        case ControlCommand_Last:
            DYSV17F_PutChar(0xAF);
            break;
        case ControlCommand_Next:
            DYSV17F_PutChar(0xB0);
            break;
        case ControlCommand_VoiceUp:
            DYSV17F_PutChar(0xBE);
            break;
        case ControlCommand_VoiceDown:
            DYSV17F_PutChar(0xBF);
            break;
        case ControlCommand_Over:
            DYSV17F_PutChar(0xBA);
            break;
        default:
            break;
    }
}

uint8_t DYSV17F_GetCommand(uint8_t CommandType)
{
    uint8_t Data[6]={0};
    DYSV17F_PutChar(0xAA);
    DYSV17F_PutChar(CommandType);
    DYSV17F_PutChar(0x00);
    switch (CommandType)
    {
        case GetCommand_Status:
            DYSV17F_PutChar(0xAB);
            break;
        case GetCommand_Drive:
            DYSV17F_PutChar(0xB3);
            break;
        case GetCommand_NowDrive:
            DYSV17F_PutChar(0xB4);
            break;
        default:
            break;
    }
    DYSV17F_Get(Data,5);
    return Data[3];
}

void DYSV17F_Set_AppointChapter(uint16_t Chapter)
{
    uint8_t chapterHigh,chapterLow;
    DYSV17F_PutChar(0xAA);
    DYSV17F_PutChar(SetCommand_AppointChapter);
    DYSV17F_PutChar(0x02);
    chapterHigh = (uint8_t)(Chapter >> 8);
    chapterLow = (uint8_t)(Chapter >> 0);
    DYSV17F_PutChar(chapterHigh);
    DYSV17F_PutChar(chapterLow);
    DYSV17F_PutChar(0xAA + SetCommand_AppointChapter + 0x02 + chapterHigh + chapterLow);
}

void DYSV17F_Set_AppointPath(uint8_t Drive,uint8_t *Path)
{
    //uint8_t mypath[]={"/01*MP3"};
    uint8_t length=0,i;
    uint32_t sm_path=0;
    DYSV17F_PutChar(0xAA);
    DYSV17F_PutChar(SetCommand_AppointPath);
    while(Path[length] != '\0')
    {
        length++;
    }
    DYSV17F_PutChar(length + 1);//长度
    DYSV17F_PutChar(Drive);//盘符
    for(i=0;i<length;i++)//路径
    {
        DYSV17F_PutChar(Path[i]);
        sm_path += Path[i];
    }
    DYSV17F_PutChar(0xAA + SetCommand_AppointPath + length + 1 + Drive + sm_path);//SM校验位
}

uint8_t DYSV17F_Set_Voice(uint8_t Voice)
{
    //默认20  范围0-30
    if(Voice >= 0 && Voice <=30)
    {
        DYSV17F_PutChar(0xAA);
        DYSV17F_PutChar(SetCommand_Voice);
        DYSV17F_PutChar(0x01);
        DYSV17F_PutChar(Voice);
        DYSV17F_PutChar(0xAA + SetCommand_Voice + 0x01 + Voice);
        return 0;
    }
    else
    {
        return 1;
    }
}